package com.nh.actionTerminal;

import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.nh.entity.GKLBook;
import com.nh.entity.GKLBookType;

public class BookAction extends GKLTerminalBaseAction {
	private Long optionId;

	public void bookTypeList() {
		Criteria criteria = this.createCriteria(GKLBookType.class);
		criteria.addOrder(Order.asc("typeOrder"));

		JsonConfig config = new JsonConfig();
		config.setIgnoreDefaultExcludes(false);
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		// 只要设置这个数组，指定过滤哪些字段。
		config.setExcludes(new String[] { "beanState", "remark" });
		// JSONSerializer.toJSON(classTypeList, config).toString()
		writResponse("{\"count\":\"" + criteria.list().size() + "\",\"list\":" + JSONSerializer.toJSON(criteria.list(), config).toString() + "}");
	}

	public void bookListByType() {
		Criteria criteria = this.createCriteria(GKLBook.class);
		criteria.addOrder(Order.asc("orderInfo"));
		criteria.add(Restrictions.eq("classType.id", optionId));

		JsonConfig config = new JsonConfig();
		config.setIgnoreDefaultExcludes(false);
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		// 只要设置这个数组，指定过滤哪些字段。
		config.setExcludes(new String[] { "beanState", "remark", "classType", "orderInfo", "desciption" });
		// JSONSerializer.toJSON(classTypeList, config).toString()
		writResponse("{\"count\":\"" + criteria.list().size() + "\",\"list\":" + JSONSerializer.toJSON(criteria.list(), config).toString() + "}");
	}

	/**
	 * 根据图书ID取内容
	 */
	public void bookDetailById() {
		GKLBook book = null;
		try {
			book = this.getBaseService().findById(optionId, GKLBook.class);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (null == book) {
			book = new GKLBook();
		}

		JsonConfig config = new JsonConfig();
		config.setIgnoreDefaultExcludes(false);
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		// 只要设置这个数组，指定过滤哪些字段。
		config.setExcludes(new String[] { "beanState", "remark", "classType", "orderInfo", });
		// JSONSerializer.toJSON(classTypeList, config).toString()
		writResponse(JSONSerializer.toJSON(book, config).toString());
	}

	public Long getOptionId() {
		return optionId;
	}

	public void setOptionId(Long optionId) {
		this.optionId = optionId;
	}
}
